/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.Gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Formatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
import org.helstius.chem.*;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Canvas;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class PercentCompositionForm extends JFrame {
	private JTextField textField;

	JTextArea textArea;
	JLabel lblMolarmass;

	int initialwidth, initialheight, initialfont;
	private JPanel panel;
	private JLabel lblGivenMass;
	private JTextField GivenMassTextBox;
	private JLabel lblComputedMass;
	private Canvas canvas;
	ProperBarChart chartee;
	PetalDiagram petally;
	private JComboBox charttypebox;
	/**
	 * If this is true, then the petal chart needs to be removed before the bar
	 * chart can be added once again.
	 */
	boolean petalchartshown = false;
	/**
	 * If this is true, then the barchart needs to be removed before rendering
	 * either none or the petalchart.
	 */
	boolean barchartshown = false;
	private GridBagConstraints gbc_canvas;

	/**
	 * Create the frame.
	 */
	public PercentCompositionForm() {
		setBounds(100, 100, 522, 364);
		initialheight = getHeight();
		initialwidth = getWidth();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 32, 316, 202, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 174, 174, 14, 23, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		textArea = new JTextArea();
		textArea.setEditable(false);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridheight = 3;
		gbc_textArea.gridwidth = 2;
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.insets = new Insets(0, 0, 5, 5);
		gbc_textArea.gridx = 0;
		gbc_textArea.gridy = 0;
		getContentPane().add(textArea, gbc_textArea);

		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 2;
		gbc_panel.gridy = 1;
		getContentPane().add(panel, gbc_panel);

		lblGivenMass = new JLabel("Given Mass");

		GivenMassTextBox = new JTextField();
		lblGivenMass.setLabelFor(GivenMassTextBox);
		GivenMassTextBox.setText("0");
		GivenMassTextBox.setColumns(10);

		lblComputedMass = new JLabel("Computed Mass:");

		JPanel panel_1 = new JPanel();

		charttypebox = new JComboBox();
		charttypebox.setModel(new DefaultComboBoxModel(new String[] { "None",
				"Petal Chart", "Bar Chart" }));
		charttypebox.setSelectedIndex(0);

		JLabel lblChartType = new JLabel("Chart Type");
		lblChartType.setLabelFor(charttypebox);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(7)
																.addComponent(
																		lblGivenMass,
																		GroupLayout.DEFAULT_SIZE,
																		60,
																		Short.MAX_VALUE)
																.addGap(110))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		GivenMassTextBox,
																		GroupLayout.DEFAULT_SIZE,
																		80,
																		Short.MAX_VALUE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addComponent(
																		lblComputedMass,
																		GroupLayout.DEFAULT_SIZE,
																		83,
																		Short.MAX_VALUE)))
								.addGap(10))
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap(12, Short.MAX_VALUE)
								.addComponent(lblChartType)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(charttypebox,
										GroupLayout.PREFERRED_SIZE, 105,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
				.addGroup(
						Alignment.LEADING,
						gl_panel.createSequentialGroup()
								.addComponent(panel_1,
										GroupLayout.DEFAULT_SIZE, 179,
										Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(8)
								.addComponent(lblGivenMass)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(8)
																.addComponent(
																		lblComputedMass))
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGap(18)
																.addComponent(
																		GivenMassTextBox,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(lblChartType)
												.addComponent(
														charttypebox,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_1,
										GroupLayout.DEFAULT_SIZE, 187,
										Short.MAX_VALUE)));
		panel_1.setLayout(new BorderLayout(0, 0));
		panel.setLayout(gl_panel);

		gbc_canvas = new GridBagConstraints();
		gbc_canvas.fill = GridBagConstraints.BOTH;
		gbc_canvas.insets = new Insets(0, 0, 5, 0);
		gbc_canvas.gridx = 2;
		gbc_canvas.gridy = 2;
		// getContentPane().add(canvas, gbc_canvas);

		lblMolarmass = new JLabel("MolarMass");
		GridBagConstraints gbc_lblMolarmass = new GridBagConstraints();
		gbc_lblMolarmass.gridwidth = 2;
		gbc_lblMolarmass.anchor = GridBagConstraints.NORTH;
		gbc_lblMolarmass.insets = new Insets(0, 0, 5, 5);
		gbc_lblMolarmass.gridx = 0;
		gbc_lblMolarmass.gridy = 3;
		getContentPane().add(lblMolarmass, gbc_lblMolarmass);

		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.insets = new Insets(0, 0, 0, 5);
		gbc_textField.gridx = 0;
		gbc_textField.gridy = 4;
		getContentPane().add(textField, gbc_textField);
		textField.setColumns(10);
		JButton btnCalculate = new JButton("Calculate");
		GridBagConstraints gbc_btnCalculate = new GridBagConstraints();
		gbc_btnCalculate.fill = GridBagConstraints.BOTH;
		gbc_btnCalculate.gridheight = 2;
		gbc_btnCalculate.anchor = GridBagConstraints.NORTHWEST;
		gbc_btnCalculate.gridx = 2;
		gbc_btnCalculate.gridy = 3;
		getContentPane().add(btnCalculate, gbc_btnCalculate);
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Parser t = new Parser();
				StringBuilder a = new StringBuilder();
				// Formatter cc = new Formatter(a, Locale.US);
				HashMap<String, Float> f = t.getpercentages(textField.getText());
				PercentCompositionForm.this.textArea.setText("");
				for (String q : f.keySet()) {
					a.append(q);
					a.append(":");
					a.append(f.get(q) * 100);
					a.append("%");
					a.append("\n");
				}
				Integer[] fff = new Integer[2];
				fff = t.getcounts(
						PercentCompositionForm.this.textField.getText())
						.values().toArray(new Integer[0]);
				ChemicalEquation aa = new ChemicalEquation(textField.getText());
				textArea.setText(a.toString());
				PercentCompositionForm.this.lblMolarmass.setText("Molar Mass: "
						+ t.parsechem(textField.getText()));
				PercentCompositionForm.this.lblComputedMass.setText(Float.toString(Float.parseFloat((GivenMassTextBox
						.getText() != "") ? GivenMassTextBox.getText() : "0")
						/ t.parsechem(textField.getText())));
				List<String> nat = new LinkedList<String>();
				List<Integer> dat = new LinkedList<Integer>();
				HashMap<String, Integer> getcounts = t
						.getcounts(PercentCompositionForm.this.textField
								.getText());
				if (charttypebox.getSelectedIndex() == 2) {
					if (petalchartshown) {
						getContentPane().remove(petally);
						petalchartshown = false;
					}
					if (chartee == null)
						PercentCompositionForm.this.chartee = new ProperBarChart();
					for (String cc : getcounts.keySet()) {
						nat.add(cc);
						dat.add(getcounts.get(cc));
					}
					PercentCompositionForm.this.chartee.AddData(nat, dat);
					if (!barchartshown) {
						getContentPane().add(chartee, gbc_canvas);
						barchartshown = true;
					}
				} else if (charttypebox.getSelectedIndex() == 1) {
					if (barchartshown) {
						getContentPane().remove(chartee);
						barchartshown = false;
					}
					if (petally == null)
						petally = new PetalDiagram();
					for (String cc : getcounts.keySet()) {
						dat.add(getcounts.get(cc));
					}

					petally.setData(dat);
					if (!petalchartshown) {
						getContentPane().add(petally, gbc_canvas);
						petalchartshown = true;
					}
					// otherwise, the charttype is set to none.
				} else {
					if (barchartshown) {
						getContentPane().remove(chartee);
						barchartshown = false;
					}
					if (petalchartshown) {
						getContentPane().remove(petally);
						petalchartshown = false;
					}
				}
				getContentPane().validate();
			}
		});
		initialfont = textArea.getFont().getSize();
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				float multwidth = getWidth() / initialwidth;
				float multheight = getHeight() / initialheight;
				textArea.setFont(textArea.getFont().deriveFont(
						initialfont * multheight));
			}
		});
	}
}
